package test.web;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.junit.Test;

import discard.Http3;
import hyl.core.MyDate;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Headers;
import okhttp3.Response;
import okhttp3.ResponseBody;

public class DemoHttp3 {

	public static void main(String[] arrg) {
		DemoHttp3 dh = new DemoHttp3();
		dh.test5();
		// dh.test4();
		// dh.test1();
		// dh.test2();
		// dh.test3();
	}

	public void test1() {
		Http3 ok3 = new Http3();
		Map<String, String> postps = new HashMap<String, String>();
		try {
			ok3.url("http://httpbin.org/delay/2").addHead("Connection", "close").addHead("Connection", "keep-alive")
					.addHead("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:63.0) Gecko/20100101 Firefox/63.0")
					.postMap(postps).execWithCall(new Callback() {
						@Override
						public void onFailure(Call call, IOException e) {
							System.out.println("Response err: " + e.getMessage());
						}

						@Override
						public void onResponse(Call call, Response response) throws IOException {
							System.out.println("Response completed: " + response);
							try (ResponseBody responseBody = response.body()) {
								if (!response.isSuccessful())
									throw new IOException("Unexpected code " + response);
								Headers responseHeaders = response.headers();
								for (int i = 0, size = responseHeaders.size(); i < size; i++) {
									System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i));
								}
								System.out.println(responseBody.string());
							}

							System.out.println("Response 1 response:          " + response);
							System.out.println("Response 1 cache response:    " + response.cacheResponse());
							System.out.println("Response 1 network response:  " + response.networkResponse());

						}

					});
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void test2() {
		Http3 ok3 = new Http3();
		Map<String, String> postps = new HashMap<String, String>();
		try {
			String str = ok3.url("http://httpbin.org/delay/2")
					// .addHead("Accept-Encoding", "gzip, deflate")
					.addHead("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
					.addHead("Upgrade-Insecure-Requests", "1")
					// .addHead("Host", "httpbin.org")
					.addHead("Accept-Language", "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2")
					.addHead("Connection", "keep-alive")
					.addHead("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:63.0) Gecko/20100101 Firefox/63.0")
					.postMap(postps).execWithResult();
			System.out.println(str);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void test3() {
		Http3 ok3 = new Http3();
		Map<String, String> postps = new HashMap<String, String>();
		try {
			String str = ok3.url("http://httpbin.org/delay/2").postMap(postps).execWithResult();
			System.out.println(str);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void test4() {
		System.out.println(Http3.getOnce("http://httpbin.org/delay/2"));
	}

	public void test5() {
		Http3 ok3 = new Http3();
		Map<String, String> postps = new HashMap<String, String>();
		try {
			String str = ok3.url("http://httpbin.org/delay/2").postMap(postps).execWithResult();
			System.out.println(str);
			MyDate.sleep(2000);
			str = ok3.url("http://httpbin.org/delay/1").postMap(postps).execWithResult();
			System.out.println(str);
			MyDate.sleep(2000);
			str = ok3.url("http://httpbin.org/delay/3").postMap(postps).execWithResult();
			System.out.println(str);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
